package com.iflytek.autofly.setting.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

/**
 * @author yutian2@iflytek.com
 */
public class CommonUtil {

    /**
     * 判断是否已经联网
     *
     * @param context 上下文
     * @return
     */
    public static boolean isConnectNetwork(Context context) {
        ConnectivityManager cManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable() && info.isConnected()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据手机分辨率从dp转成px
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f) - 15;
    }

    /**
     * 格式化导航剩余时间
     *
     * @param time
     * @return
     */
    public static String parseTimeForNavi(String time) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }

        int value = Integer.parseInt(time);

        int h = 0;
        int d = 0;
        int s = 0;
        int temp = value % 3600;
        if (value > 3600) {
            h = value / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = value / 60;
            if (value % 60 != 0) {
                s = value % 60;
            }
        }

        if (h == 0 && d == 0 && s == 0) {
            return "0秒";
        } else if (h == 0 && d == 0) {
            return s + "秒";
        } else if (h == 0 && d != 0) {
            return d + "分钟";
        } else if (h != 0 && d == 0) {
            return h + "小时";
        } else if (h != 0 && d != 0) {
            return h + "小时" + d + "分钟";
        }

        return h + "时" + d + "分" + s + "秒";

    }

    /**
     * 格式化导航剩余距离
     *
     * @param length
     * @return
     */
    public static String parseLengthForNavi(String length) {
        if (TextUtils.isEmpty(length)) {
            return "";
        }

        int value = Integer.parseInt(length);

        if (value < 1000) {
            return value + "米";
        } else {
            double v = value / 1000d;
            DecimalFormat df = new java.text.DecimalFormat("#.0");
            String s = df.format(v);
            if (s.endsWith(".0")) {
                s = s.substring(0, s.indexOf('.'));
            }
            return s + "公里";
        }

    }

    public static void toast(Context context, CharSequence text, int duration) {
        TextView tv = new TextView(context);
        tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tv.setTextSize(25);
        tv.setGravity(Gravity.CENTER);
        tv.setText(text);
        Toast t = new Toast(context);
        t.setView(tv);
        t.setDuration(duration);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
    }
}
